package com.itheima.utils;

import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;

/**
 * @author wangjiexin
 */
public class DateTimeUtils {

    public static Integer birthdayByAge(String birthday){
        if (StringUtils.isEmpty(birthday)){
            //如果为空的话，返回0 表示差距0岁
            return 0;
        }
        //获取当前时间
        LocalDate now = LocalDate.now();
        //将生日转换为可运算的时间
        LocalDate birthdayDate = LocalDate.parse(birthday, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        //计算两个时间的间隔
        Period between = Period.between(now, birthdayDate);
        //获取间隔的年份 取绝对值
        return Math.abs(between.getYears());
    }

    public static Integer getScore(Integer myAge,Integer age){

        // 我的年龄 – 他的年龄=取整（差异) 取绝对值
        int ageBetween =Math.abs(myAge-age);

        //缘分值初始100 差一岁减X分
        int res = 100 - (ageBetween * 3);
        //如果计算出来的缘分值小于0 那么返回0 缘分值不小于0那么就返回对应的值
        return res<0? 0:res;
    }

}
